package business.impl;

import java.util.List;

import model.TNovel;
import model.TNovelContent;
import model.VNovelContent;
import business.basic.HibBaseDAOImpl;
import business.basic.iHibBaseDAO;
import business.dao.NovelContentDAO;

public class NovelContentDAOImpl implements NovelContentDAO {
	iHibBaseDAO hdao = new HibBaseDAOImpl();
	@Override
	public int maxOrdernum(int novelid) {
		String hql = "select max(ordernum) from VNovelContent "
				+ "where novelid = ?";
		Object[] para = {novelid};
		return hdao.selectValue(hql,para);
	}
	
	@Override
	public int produceValidSortId(int novelid) {
		String hql = "select max(ordernum) from TNovelContent "
				+ "where novelid = ?";
		Object[] para = {novelid};
		return hdao.selectValue(hql,para)+1;
	}

	@Override
	public int addContent(TNovelContent ncontent) {
		String procName ="up_addNovelContent(?,?,?,?,?)";
		Object para[]={ncontent.getNovelid(),ncontent.getChaptername(),ncontent.getTextcontent(),
				ncontent.getCharge(),ncontent.getOrdernum()};
		return (Integer)hdao.executeProduce(procName,para);
	}

	@Override
	public boolean deleteContent(int chapterid) {
		String procName = "up_deleteNovelContent(?)";
		Object [] para = {chapterid};
		int row  = (Integer) hdao.executeProduce(procName,para);
		if (row>0) {
			return true;
		}
		else {
			return false;
		}
	}

	@Override
	public boolean deleteContentsBelongNovel(int novelid) {
		TNovel novel = (TNovel) hdao.findById(TNovel.class, novelid);
		Object obj = hdao.delete(novel);
		if(obj!=null){
			return true;
		}else {
			return false;
		}
	}

	@Override
	public boolean updateNovelContent(TNovelContent ncontent) {
		return hdao.update(ncontent);
	}

	@Override
	public VNovelContent getContnentByName(int chapterid) {
		return (VNovelContent) hdao.findById(VNovelContent.class, chapterid);
	}

	@Override
	public TNovelContent getTContnentByName(int chapterid) {
		return (TNovelContent) hdao.findById(TNovelContent.class, chapterid);
	}

	@Override
	public int getNovelContentByNum(int novelid) {
		String sql = "select count(*) from VNovelContent where novelid = ? and auditstatus = 'True'";
		Object [] para= {novelid};
		return hdao.selectValue(sql,para);
	}

	@Override
	public List<VNovelContent> getContentListByNovelName(int novelid) {
		String hql = "from VNovelContent where novelid = ? and auditstatus = 'True' order by ordernum asc";
		Object [] para= {novelid};
		return hdao.select(hql,para);
	}

	@Override
	public VNovelContent getNewNovelContentByNovelName(int novelid) {
		String sql = "select max(chapterid) from VNovelContent where novelid = ? and auditstatus = 'True'";
		Object [] pareObjects = {novelid};
		return (VNovelContent) hdao.select(sql,pareObjects);
	}

	@Override
	public List<VNovelContent> getRecentUpdates() {
		String hql = "from VNovelContent where auditstatus = 'True' order by createtime desc";
		return hdao.select(hql);
	}

	@Override
	public List<VNovelContent> getNovelContentAddCount(int ordernum, int novelid) {
		int ordercount = ordernum + 1;
		String hql = "from VNovelContent where ordernum = ? and novelid=? and auditstatus = 'True'";
		Object[] para={ordercount,novelid};
		return hdao.select(hql,para);
	}

	@Override
	public List<VNovelContent> getNovelContentFristCount(int ordernum, int novelid) {
		int ordercount = ordernum - 1;
		String hql = "from VNovelContent where ordernum = ? and novelid=? and auditstatus = 'True'";
		Object[] para={ordercount,novelid};
		return hdao.select(hql,para);
	}

	@Override
	public List<VNovelContent> getNovelContentOneCount(int novelid) {
		String hql = "from VNovelContent where novelid = ? and ordernum = 1 and auditstatus = 'True'";
		Object[] para={novelid};
		return hdao.select(hql,para);
		//return  novelContents;
	}

	@Override
	public List<VNovelContent> getAuthorRecentUpdates() {
		String hql = "from VNovelContent order by createtime desc";
		return hdao.select(hql);
	}

	@Override
	public List<VNovelContent> getAuthorContentListByNovelName(int novelid) {
		String hql = "from VNovelContent where novelid = ?  order by ordernum asc";
		Object [] para= {novelid};
		return hdao.select(hql,para);
	}

	


	

}
